package com.project.common.domain.po;


import com.project.common.domain.BaseVersionDO;
import com.project.common.domain.Convert;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeMap;
import org.modelmapper.spi.DestinationSetter;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * BasePoConvert
 *
 */
public class BasePoConvert extends Convert {
    ModelMapper mapper = Convert.getConvert();

    /**
     * convertTo
     *
     * @param destinationType
     */
    public void convertTo(Object destinationType) {
        Assert.notNull(destinationType, "参数不能为NULL");
        mapper.map(this, destinationType);
    }

    /**
     * 转对象
     *
     * @param <T>
     * @return T
     */
    public <T> T convertTo(Class<T> t) {
        Assert.notNull(t, "参数不能为NULL");
        return mapper.map(this, t);
    }

    /**
     * 转DO
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T extends BaseVersionDO> T convertToDo(Class<T> t) {
        return convertTo(t);
    }

    /**
     * 转对象
     *
     * @param <T>
     * @return T
     */
    public <T> T skipConvertTo(Class<T> destinationType, DestinationSetter... setter) {
        Assert.notNull(setter, "参数不能为NULL");
        List<DestinationSetter> list = Arrays.asList(setter);
        Assert.notEmpty(list, "参数不能为NULL");
        TypeMap<BasePoConvert, T> typeMap = mapper.createTypeMap(this, destinationType);
        list.forEach(s -> {
            typeMap.addMappings(m -> m.skip(s));
        });
        return mapper.map(this, destinationType);
    }

    /**
     * 转对象
     *
     * @param <T>
     * @return T
     */
    public <T> T skipConvertTo(Class<T> destinationType, List<DestinationSetter> setter) {
        Assert.notEmpty(setter, "参数不能为空");
        TypeMap<BasePoConvert, T> typeMap = mapper.createTypeMap(this, destinationType);
        setter.forEach(s -> {
            typeMap.addMappings(m -> m.skip(s));
        });
        return mapper.map(this, destinationType);
    }

}
